package com.xjrsoft.module.ureport.service.impl;

import cn.hutool.core.date.DateTime;
import com.xjrsoft.core.tool.node.ForestNodeMerger;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.module.base.entity.XjrBaseModule;
import com.xjrsoft.module.base.service.IXjrBaseDataItemDetailService;
import com.xjrsoft.module.base.service.IXjrBaseModuleService;
import com.xjrsoft.module.base.vo.ReportInfoManagerVo;
import com.xjrsoft.module.base.vo.RptFileManagerVo;
import com.xjrsoft.module.ureport.dto.ReportInfoDto;
import com.xjrsoft.module.ureport.entity.XjrReportInfo;
import com.xjrsoft.module.ureport.entity.XjrRptFile;
import com.xjrsoft.module.ureport.entity.XjrRptRelation;
import com.xjrsoft.module.ureport.mapper.XjrReportInfoMapper;
import com.xjrsoft.module.ureport.service.IXjrReportInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.module.ureport.vo.ReportInfoVo;
import com.xjrsoft.module.ureport.vo.RptFileInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwq
 * @since 2021-04-14
 */
@Service
public class XjrReportInfoServiceImpl extends ServiceImpl<XjrReportInfoMapper, XjrReportInfo> implements IXjrReportInfoService {

    @Autowired
    private IXjrBaseModuleService xjrBaseModuleService;

    @Autowired
    private IXjrReportInfoService iXjrReportInfoService;

    @Autowired
    private IXjrBaseDataItemDetailService dataItemDetailService;


    @Override
    public List<ReportInfoManagerVo> tree() {
        // 查询报表分类的数据字典明细
        List<ReportInfoManagerVo> detailList = BeanUtil.copyList(dataItemDetailService.getDataItemDetailListByCode("ReportSort", null), ReportInfoManagerVo.class);
        List<ReportInfoVo> reportInfoVoList = BeanUtil.copyList(iXjrReportInfoService.list(), ReportInfoVo.class);
        iXjrReportInfoService.buildReportInfoTree(detailList, reportInfoVoList);
       return ForestNodeMerger.merge(detailList);
    }

    @Override
    public void buildReportInfoTree(List<ReportInfoManagerVo> detailList, List<ReportInfoVo> rptFileInfoList) {
        for (ReportInfoManagerVo reportInfoManagerVo : detailList) {
            for (ReportInfoVo reportInfoVo : rptFileInfoList) {
                if (StringUtils.equalsIgnoreCase(reportInfoManagerVo.getItemValue(), reportInfoVo.getType())) {
                    reportInfoManagerVo.getChildren().add(reportInfoVo);
                }
            }
        }
    }


    @Override
    public boolean saveReportInfo(ReportInfoDto dto) {
        XjrReportInfo xjrReportInfo = BeanUtil.copy(dto, XjrReportInfo.class);
        xjrReportInfo.setDeleteMark(0);
        xjrReportInfo.setEnabledMark(1);
        return baseMapper.insert(xjrReportInfo) > 0;
    }

    @Override
    public boolean updateReportInfo(String id, ReportInfoDto dto) {
        XjrReportInfo xjrReportInfo = BeanUtil.copy(dto, XjrReportInfo.class);
        xjrReportInfo.setId(id);
        return baseMapper.updateById(xjrReportInfo) > 0;
    }

    @Override
    public boolean deleteReportInfo(String ids) throws Exception {
        List<String> idList = Arrays.asList(ids.split(StringPool.COMMA));
        if (CollectionUtil.isNotEmpty(idList)) {
            return this.removeByIds(idList);
        }else {
            throw new Exception("请选择要删除的信息!!");
        }
    }
}
